package com.blackcat.v2.demo;

public class Car implements MyLifecycle {

    private Light leftLight ; //左大灯
    private Light rightLight ; //右大灯

    private Engine engine ; //引擎

    private MyLifecycleSupport lifecycle = new MyLifecycleSupport(this);

    public void run(){
        start();
        System.out.println("桑塔拉跑起来了...");
    }

    public void shutdown(){
        stop();
        System.out.println("桑塔拉停止了...");
    }

    @Override
    public void addLifecycleListener(MyLifecycleListener listener) {
        lifecycle.addLifecycleListener(listener);
    }

    @Override
    public MyLifecycleListener[] getLifecycleEvents() {
        return lifecycle.getLifecycleEvents();
    }

    @Override
    public void start() {
        lifecycle.fireLifecycleEvent(MyLifecycle.BEFORE_START, null);
        //先初始化其他组件
        if(engine != null && engine instanceof MyLifecycle){
            ((MyLifecycle)engine).start();
        }
        if(leftLight != null && leftLight instanceof MyLifecycle){
            ((MyLifecycle)leftLight).start();
        }
        if(rightLight != null && rightLight instanceof MyLifecycle){
            ((MyLifecycle)rightLight).start();
        }

        System.out.println("桑塔拉正在启动...");
        lifecycle.fireLifecycleEvent(MyLifecycle.AFTER_START, null);
    }

    @Override
    public void stop() {
        //先停止其他组件
        if(engine != null && engine instanceof MyLifecycle){
            ((MyLifecycle)engine).stop();
        }
        if(leftLight != null && leftLight instanceof MyLifecycle){
            ((MyLifecycle)leftLight).stop();
        }
        if(rightLight != null && rightLight instanceof MyLifecycle){
            ((MyLifecycle)rightLight).stop();
        }

        lifecycle.fireLifecycleEvent(MyLifecycle.BEFORE_STOP, null);
        System.out.println("桑塔拉正在停止...");
        lifecycle.fireLifecycleEvent(MyLifecycle.AFTER_STOP, null);
    }

    public Light getLeftLight() {
        return leftLight;
    }

    public void setLeftLight(Light leftLight) {
        this.leftLight = leftLight;
    }

    public Light getRightLight() {
        return rightLight;
    }

    public void setRightLight(Light rightLight) {
        this.rightLight = rightLight;
    }

    public Engine getEngine() {
        return engine;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }


}
